﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UtilsLib.UtilsType;

namespace UtilsLib.UtilsLog
{
    public static class LogToConsole
    {
        private static DateTime lastLog;
        private static bool _isConsoleApp = false;

        internal static void Initialise()
        {
            try
            {
                _isConsoleApp = true;
                WriteConsoleLine(TypeMessage.AppStart, "", false);
                lastLog = DateTime.Now;
            }
            catch
            {
                _isConsoleApp = false;
            }
        }

        internal static void WriteConsoleLine(TypeMessage type, string message, bool elapsed)
        {
            if (_isConsoleApp)
            {
                if (elapsed)
                {
                    TimeSpan diff = DateTime.Now.Subtract(lastLog);
                    string elapsedStr = (diff.Hours > 0 ? diff.Hours + "h " : "") +
                                        (diff.Minutes > 0 ? diff.Minutes + "mn " : "") +
                                        (diff.Seconds > 0 ? diff.Seconds + "s " : "") +
                                        (diff.Milliseconds > 0 ? diff.Milliseconds + "ms " : "");
                    elapsedStr = (elapsedStr == "" ? " " : " [" + elapsedStr + "] ");

                    Console.WriteLine(DateTime.Now.ToString("[HH:mm:ss: fff]") + " [" + type.ToString() + "]" + elapsedStr + message);
                    lastLog = DateTime.Now;
                }
                else
                {
                    Console.WriteLine(DateTime.Now.ToString("[HH:mm:ss: fff]") + " [" + type.ToString() + "] " + message);
                }

            }
        }

        public static void WriteConsoleMessage(string message, bool elapsed = false)
        {
            WriteConsoleLine(TypeMessage.Message, message, elapsed);
        }

        public static void WriteConsoleException(string message, Exception ex, bool elapsed = false)
        {
            string fullMessage = message + "  |  Exception Description : " + ex.Message;

            WriteConsoleLine(TypeMessage.Message, message, elapsed);
        }

        public static void WriteConsoleWarning(string message, bool elapsed = false)
        {
            WriteConsoleLine(TypeMessage.Warning, message, elapsed);
        }

        public static void WriteConsoleError(string message, bool elapsed = false)
        {
            WriteConsoleLine(TypeMessage.Error, message, elapsed);
        }

    }

}
